<#
.Author
	cw50406
.Version
	1.3 -rewrite code to be faster for bulk vdi info collect (200s for 1482 vdis)
.ROLE
   ICG Support Team
.DESCRIPTION
	This is used to collect single vdi config infomation with below types, the csv file will be saved in the same directory:
		1. software version
		2. service running status
		3. ram/c driver size
	Meanings for short valus:
		R-1.7.0.101<-->N-java.exe means: found registry version but not found software exe
		R-1.7.0.101<-->N- means: the 'InstallLocation' value in its registry is empty
		R-1.7.0.91<-->P-1.7.0.101 means: registry version 1.7.0.91 not matches software exe version 1.7.0.101
		R-1.7.0.91<-->N-PV means: found registry version 1.7.0.91 but not found software exe version
		P-1.7.0.101<-->N-RV means: found software exe version 1.7.0.101 but not found registry version
.EXAMPLE 
	.\get_single_vdi_info.ps1
		this will save the results in a new random csv file
	.\get_single_vdi_info.ps1 -csv_file_name.csv
		this will save the results in the given csv file
#>
param(
	[Parameter(Mandatory=$false)]
	[string]$temp_csv_file
)
#<----------------------------------initilize function & parameters---------------------------------->
Function Get-NETVersion($reg,$net_version){
	Foreach( $r in ((dir $reg -ea 0).name -imatch '^HKEY_LOCAL_MACHINE')){
		$r=$r -replace("^HKEY_LOCAL_MACHINE","HKLM:")
		If( $r -ne $reg){
			If( ($version=(gp $r -ea 0).Version) -gt $net_version){
				$net_version=$version
			}else{$net_version=Get-NETVersion $r $net_version}
		}
	}
	return $net_version
}
$this_path = Split-Path -Parent ($MyInvocation.MyCommand.Definition)
$reg32_path="HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$reg64_path="HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
$software_pros = [ordered]@{
	"VDI"=$env:COMPUTERNAME;
	"Domain"=(Get-WmiObject Win32_ComputerSystem).Domain.Split(".")[0];
	"AA"=$null;
	"Chrome"=$null;
	"IE"=$null
	"Java"=$null;
	"JavaWebStart"=$null;
	"NetProbe"=$null;
	".NET"=$null;
	"PowerShell"=$null;
	"VNC"=$null;
	"JettyService"=$null;
	"UDeployService"=$null;
	"RAM"=$null;
	"C:"=$null;
}
#<----------------------------------1. set AA version
$software_pros.Set_Item("AA",(gp "$reg32_path\{CB6A8B9C-0E2A-4746-B096-438929F00A15}" -ea 0).DisplayVersion)
#<----------------------------------2. set Chrome version
$software_pros.Set_Item("Chrome",(gi "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ea 0).VersionInfo.ProductVersion)
#<----------------------------------3. set IE version
$software_pros.Set_Item("IE",(gp "HKLM:\SOFTWARE\Microsoft\Internet Explorer" -ea 0).svcVersion)
#<----------------------------------4. set Java version
If( $java_reg=dir "$reg32_path\"|?{$_.name -imatch '26A24AE4-039D-4CA4-87B4'}|gp -ea 0){
    $java_reg_version=$java_reg.DisplayVersion
	$java_exe=$java_reg.InstallLocation +"bin\java.exe"
	If( test-path $java_exe -ea 0 ){
		$java_exe_version=(gi $java_exe -ea 0).VersionInfo.ProductVersion
		If( $java_reg_version -ne $java_exe_version ){
			$java_version="R-$java_reg_version<-->E-$java_exe_version"
		}
	}else{$java_reg_version="R-$java_reg_version<-->N-java.exe"}
	$software_pros.Set_Item("Java",$java_reg_version)
}
#<----------------------------------5. set Java Web Start version
If( $java_reg_version ){
	If( $jws_reg=dir $reg32_path|Get-ItemProperty|?{$_.displayname -imatch "^Java Web Start" -and $_.InstallLocation -ne $null} ){
	    $jws_reg_version=$jws_reg.DisplayVersion
		$jws_exe=$jws_reg.InstallLocation +"javaws.exe"
		If( test-path $jws_exe -ea 0 ){
			$jws_exe_version=(gi $jws_exe -ea 0).VersionInfo.ProductVersion
		    If( $jws_exe_version -inotmatch $java_reg_version){
		        $jws_reg_version="R-$jws_reg_version<-->E-$jws_exe_version"
		    }
		}else{$jws_reg_version="R-$jws_reg_version<-->N-javaws.exe"}
	}
	$software_pros.Set_Item("JavaWebStart",$jws_reg_version)
}
#<----------------------------------6. set NetProbe version
$software_pros.Set_Item("NetProbe",(gp "$reg32_path\NetprobeNT-7136 GA3.7.1_is1" -ea 0)."Inno Setup: Setup Version")
#<----------------------------------7. set .NET version
$net_version='0'
Foreach( $reg in ((dir "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP").name -imatch '\\v')){
	$reg=$reg -replace("^HKEY_LOCAL_MACHINE","HKLM:")
	If( ($version=(gp $reg -ea 0).Version) -gt $net_version){
		$net_version=$version
	}else{$net_version=Get-NETVersion $reg $net_version}
}
$software_pros.Set_Item(".NET",$net_version)
#<----------------------------------8. set PowerShell version
$software_pros.Set_Item("PowerShell",$Host.Version.ToString())
#<----------------------------------9. set VNC version
If( ($vnc_version=(gi "C:\Program Files (x86)\UltraVNC\winvnc_32.exe" -ea 0).VersionInfo.FileVersion) -eq $null){
    $vnc_version="N-winvnc_32.exe"
}
$software_pros.Set_Item("VNC",$vnc_version)
#<----------------------------------10. set JettyService status
$software_pros.Set_Item("JettyService",(Get-Service JettyService -ea 0).Status)
#<----------------------------------11.set UDeployService status
$software_pros.Set_Item("UDeployService",(Get-Service UCD_Agent -ea 0).Status)
#<----------------------------------12.set RAM size
$software_pros.Set_Item("RAM",((Get-WmiObject CIM_PhysicalMemory).Capacity|Measure-Object -Sum|%{"{0:0.00}" -f ($_.sum / 1gb)}))
#<----------------------------------13.set C driver size
$software_pros.Set_Item("C:",("{0:0.00}" -f ((Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'").Size / 1gb)))
#-------------------------------------------> check port 25
# -ErrorAction Ignore cannot be used in PS2.0, Export-Csv in ps2.0 do not contain -append parameter
If( [string]::IsNullOrWhiteSpace($temp_csv_file) ){
	$temp_csv_file="{0:yyyyMMddHHmmss}-{1}.csv" -f (Get-Date),(Get-Random -Maximum 10000)
}
del $this_path\*.csv
$temp_csv_file="$this_path\$temp_csv_file"
$rst= New-Object PSObject -Property $software_pros
try{$rst|Export-Csv $temp_csv_file -UseCulture -NoTypeInformation -Encoding UTF8 -Force}catch{}
